{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 2.7 分组运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.108393Z",
     "end_time": "2024-05-09T20:43:35.162527Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum =  10\n",
      "mean =  2.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "s = pd.Series(np.arange(5))\n",
    "print(\"sum = \", np.sum(s))\n",
    "print(\"mean = \", np.mean(s))"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.简单的统计运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "                GDP  Population\nCity_Name                      \nSHANGHAI   27466.15     2419.70\nBEIJING    24899.30     2172.90\nGUANGZHOU  19610.90     1350.11\nSHENZHEN   19492.60     1137.87\nTIANJIN    17885.39     1562.12\nCHONGQING  17558.76     3016.55\nSUZHOU     15475.09     1375.00\nCHENGDU    12170.20     1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n    <tr>\n      <th>City_Name</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SHANGHAI</th>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>BEIJING</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>GUANGZHOU</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>SHENZHEN</th>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>TIANJIN</th>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>CHONGQING</th>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>SUZHOU</th>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>CHENGDU</th>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp = pd.read_csv('gdp-population.csv')\n",
    "city_gdp = gdp.set_index('City_Name')\n",
    "city_gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.141374Z",
     "end_time": "2024-05-09T20:43:35.167397Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "GDP           154558.39\nPopulation     14626.01\ndtype: float64"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_gdp.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.167397Z",
     "end_time": "2024-05-09T20:43:35.205960Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "GDP           19319.79875\nPopulation     1828.25125\ndtype: float64"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_gdp.mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.183873Z",
     "end_time": "2024-05-09T20:43:35.261446Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "GDP           18688.995\nPopulation     1576.940\ndtype: float64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_gdp.median()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.201573Z",
     "end_time": "2024-05-09T20:43:35.274078Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "(4.0, array([3., 4., 5.]))"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.arange(9).reshape(3, 3)\n",
    "b.mean(), b.mean(axis=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.222808Z",
     "end_time": "2024-05-09T20:43:35.350975Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "City_Name\nSHANGHAI     14942.925\nBEIJING      13536.100\nGUANGZHOU    10480.505\nSHENZHEN     10315.235\nTIANJIN       9723.755\nCHONGQING    10287.655\nSUZHOU        8425.045\nCHENGDU       6880.980\ndtype: float64"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_gdp.median(axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.241000Z",
     "end_time": "2024-05-09T20:43:35.350975Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "GDP           19319.79875\nPopulation     1828.25125\ndtype: float64"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_gdp.loc['HongLouShi'] = np.nan\n",
    "city_gdp.mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.265208Z",
     "end_time": "2024-05-09T20:43:35.350975Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "                GDP   Population\ncount      8.000000     8.000000\nmean   19319.798750  1828.251250\nstd     4908.677545   645.654074\nmin    12170.200000  1137.870000\n25%    17037.842500  1368.777500\n50%    18688.995000  1576.940000\n75%    20933.000000  2234.600000\nmax    27466.150000  3016.550000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>8.000000</td>\n      <td>8.000000</td>\n    </tr>\n    <tr>\n      <th>mean</th>\n      <td>19319.798750</td>\n      <td>1828.251250</td>\n    </tr>\n    <tr>\n      <th>std</th>\n      <td>4908.677545</td>\n      <td>645.654074</td>\n    </tr>\n    <tr>\n      <th>min</th>\n      <td>12170.200000</td>\n      <td>1137.870000</td>\n    </tr>\n    <tr>\n      <th>25%</th>\n      <td>17037.842500</td>\n      <td>1368.777500</td>\n    </tr>\n    <tr>\n      <th>50%</th>\n      <td>18688.995000</td>\n      <td>1576.940000</td>\n    </tr>\n    <tr>\n      <th>75%</th>\n      <td>20933.000000</td>\n      <td>2234.600000</td>\n    </tr>\n    <tr>\n      <th>max</th>\n      <td>27466.150000</td>\n      <td>3016.550000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_gdp.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.312414Z",
     "end_time": "2024-05-09T20:43:35.350975Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.分组运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "   subject  score\n0     math     90\n1  physics     80\n2  english     70\n3     math     95\n4  physics     85\n5  english     75",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>subject</th>\n      <th>score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>math</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>physics</td>\n      <td>80</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>english</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>math</td>\n      <td>95</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>physics</td>\n      <td>85</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>english</td>\n      <td>75</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\"subject\": ['math', 'physics', 'english', 'math', 'physics', 'english'],\n",
    "                   'score': [90, 80, 70, 95, 85, 75]})\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.328991Z",
     "end_time": "2024-05-09T20:43:35.366972Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "         score\nsubject       \nenglish    145\nmath       185\nphysics    165",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n    </tr>\n    <tr>\n      <th>subject</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>english</th>\n      <td>145</td>\n    </tr>\n    <tr>\n      <th>math</th>\n      <td>185</td>\n    </tr>\n    <tr>\n      <th>physics</th>\n      <td>165</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('subject').sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.350975Z",
     "end_time": "2024-05-09T20:43:35.384299Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000002C0DB279690>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_group = df.groupby(\"subject\")\n",
    "sub_group"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.368529Z",
     "end_time": "2024-05-09T20:43:35.455575Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "3"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(sub_group)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.384299Z",
     "end_time": "2024-05-09T20:43:35.466304Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "english\n",
      "   subject  score\n",
      "2  english     70\n",
      "5  english     75\n",
      "math\n",
      "  subject  score\n",
      "0    math     90\n",
      "3    math     95\n",
      "physics\n",
      "   subject  score\n",
      "1  physics     80\n",
      "4  physics     85\n"
     ]
    }
   ],
   "source": [
    "for k, d in sub_group:\n",
    "    print(k)\n",
    "    print(d)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.397228Z",
     "end_time": "2024-05-09T20:43:35.529937Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "   subject  score\n1  physics     80\n4  physics     85",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>subject</th>\n      <th>score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>physics</td>\n      <td>80</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>physics</td>\n      <td>85</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = dict(list(sub_group))\n",
    "d['physics']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.434545Z",
     "end_time": "2024-05-09T20:43:35.529937Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "   subject  score\n1  physics     80\n4  physics     85",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>subject</th>\n      <th>score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>physics</td>\n      <td>80</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>physics</td>\n      <td>85</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_group.get_group(\"physics\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.460776Z",
     "end_time": "2024-05-09T20:43:35.614543Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "   subject  score teacher  rank\n0     math     90  Netwon     4\n1  physics     80  Netwon     5\n2  english     70  Pascal     2\n3     math     95  Netwon     9\n4  physics     85  Pascal     7\n5  english     75  Pascal     5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>subject</th>\n      <th>score</th>\n      <th>teacher</th>\n      <th>rank</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>math</td>\n      <td>90</td>\n      <td>Netwon</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>physics</td>\n      <td>80</td>\n      <td>Netwon</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>english</td>\n      <td>70</td>\n      <td>Pascal</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>math</td>\n      <td>95</td>\n      <td>Netwon</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>physics</td>\n      <td>85</td>\n      <td>Pascal</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>english</td>\n      <td>75</td>\n      <td>Pascal</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['teacher'] = ['Netwon', 'Netwon', 'Pascal', 'Netwon', 'Pascal', 'Pascal']\n",
    "df['rank'] = [4, 5, 2, 9, 7, 5]\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.487265Z",
     "end_time": "2024-05-09T20:43:35.657570Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "         score  rank\nsubject             \nenglish   72.5   3.5\nmath      92.5   6.5\nphysics   82.5   6.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n      <th>rank</th>\n    </tr>\n    <tr>\n      <th>subject</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>english</th>\n      <td>72.5</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>math</th>\n      <td>92.5</td>\n      <td>6.5</td>\n    </tr>\n    <tr>\n      <th>physics</th>\n      <td>82.5</td>\n      <td>6.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('subject').mean(numeric_only=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.509004Z",
     "end_time": "2024-05-09T20:43:35.664805Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "         score  rank\nsubject             \nenglish   72.5   3.5\nmath      92.5   6.5\nphysics   82.5   6.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n      <th>rank</th>\n    </tr>\n    <tr>\n      <th>subject</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>english</th>\n      <td>72.5</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>math</th>\n      <td>92.5</td>\n      <td>6.5</td>\n    </tr>\n    <tr>\n      <th>physics</th>\n      <td>82.5</td>\n      <td>6.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(df['subject']).mean(numeric_only=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.525224Z",
     "end_time": "2024-05-09T20:43:35.729568Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "pandas.core.series.Series"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(df['subject'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.551788Z",
     "end_time": "2024-05-09T20:43:35.752053Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "                 score  rank\nteacher subject             \nNetwon  math      92.5   6.5\n        physics   80.0   5.0\nPascal  english   72.5   3.5\n        physics   85.0   7.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>score</th>\n      <th>rank</th>\n    </tr>\n    <tr>\n      <th>teacher</th>\n      <th>subject</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">Netwon</th>\n      <th>math</th>\n      <td>92.5</td>\n      <td>6.5</td>\n    </tr>\n    <tr>\n      <th>physics</th>\n      <td>80.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">Pascal</th>\n      <th>english</th>\n      <td>72.5</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>physics</th>\n      <td>85.0</td>\n      <td>7.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['teacher', 'subject']).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.573903Z",
     "end_time": "2024-05-09T20:43:35.790813Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "teacher  subject\nNetwon   math       92.5\n         physics    80.0\nPascal   english    72.5\n         physics    85.0\nName: score, dtype: float64"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['teacher', 'subject'])['score'].mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.598211Z",
     "end_time": "2024-05-09T20:43:35.790813Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "               a         b         c         d         e         f\nfirst   0.420153  2.455168  0.768435 -0.686499 -0.865518 -0.161854\nsecond  0.297751  0.544228  0.067817 -0.513818 -0.217465 -1.654308\nthird  -0.409088 -0.087751 -1.080083  0.484670 -1.608634  0.408745\nforth   0.134988 -1.075888  0.693899  0.017387  1.985844 -0.082188\nfifth   0.453056 -0.502912  0.439248 -0.849174 -1.277251 -1.238545\nsixth  -0.872252 -0.222147  1.550124 -1.367904  0.342844 -0.839865",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>first</th>\n      <td>0.420153</td>\n      <td>2.455168</td>\n      <td>0.768435</td>\n      <td>-0.686499</td>\n      <td>-0.865518</td>\n      <td>-0.161854</td>\n    </tr>\n    <tr>\n      <th>second</th>\n      <td>0.297751</td>\n      <td>0.544228</td>\n      <td>0.067817</td>\n      <td>-0.513818</td>\n      <td>-0.217465</td>\n      <td>-1.654308</td>\n    </tr>\n    <tr>\n      <th>third</th>\n      <td>-0.409088</td>\n      <td>-0.087751</td>\n      <td>-1.080083</td>\n      <td>0.484670</td>\n      <td>-1.608634</td>\n      <td>0.408745</td>\n    </tr>\n    <tr>\n      <th>forth</th>\n      <td>0.134988</td>\n      <td>-1.075888</td>\n      <td>0.693899</td>\n      <td>0.017387</td>\n      <td>1.985844</td>\n      <td>-0.082188</td>\n    </tr>\n    <tr>\n      <th>fifth</th>\n      <td>0.453056</td>\n      <td>-0.502912</td>\n      <td>0.439248</td>\n      <td>-0.849174</td>\n      <td>-1.277251</td>\n      <td>-1.238545</td>\n    </tr>\n    <tr>\n      <th>sixth</th>\n      <td>-0.872252</td>\n      <td>-0.222147</td>\n      <td>1.550124</td>\n      <td>-1.367904</td>\n      <td>0.342844</td>\n      <td>-0.839865</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.DataFrame(np.random.randn(6, 6), columns=[\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"],\n",
    "                    index=[\"first\", \"second\", \"third\", \"forth\", \"fifth\", \"sixth\"])\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.619384Z",
     "end_time": "2024-05-09T20:43:35.830126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_3948\\2227913837.py:2: FutureWarning: DataFrame.groupby with axis=1 is deprecated. Do `frame.T.groupby(...)` without axis instead.\n",
      "  data.groupby(lst, axis=1).mean()\n"
     ]
    },
    {
     "data": {
      "text/plain": "             one       two\nfirst   1.214585 -0.571290\nsecond  0.303265 -0.795197\nthird  -0.525641 -0.238406\nforth  -0.082334  0.640348\nfifth   0.129797 -1.121657\nsixth   0.151908 -0.621642",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>first</th>\n      <td>1.214585</td>\n      <td>-0.571290</td>\n    </tr>\n    <tr>\n      <th>second</th>\n      <td>0.303265</td>\n      <td>-0.795197</td>\n    </tr>\n    <tr>\n      <th>third</th>\n      <td>-0.525641</td>\n      <td>-0.238406</td>\n    </tr>\n    <tr>\n      <th>forth</th>\n      <td>-0.082334</td>\n      <td>0.640348</td>\n    </tr>\n    <tr>\n      <th>fifth</th>\n      <td>0.129797</td>\n      <td>-1.121657</td>\n    </tr>\n    <tr>\n      <th>sixth</th>\n      <td>0.151908</td>\n      <td>-0.621642</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [\"one\", \"one\", \"one\", \"two\", \"two\", \"two\"]\n",
    "data.groupby(lst, axis=1).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.650376Z",
     "end_time": "2024-05-09T20:43:35.830126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "             one       two\nfirst   1.214585 -0.571290\nsecond  0.303265 -0.795197\nthird  -0.525641 -0.238406\nforth  -0.082334  0.640348\nfifth   0.129797 -1.121657\nsixth   0.151908 -0.621642",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>first</th>\n      <td>1.214585</td>\n      <td>-0.571290</td>\n    </tr>\n    <tr>\n      <th>second</th>\n      <td>0.303265</td>\n      <td>-0.795197</td>\n    </tr>\n    <tr>\n      <th>third</th>\n      <td>-0.525641</td>\n      <td>-0.238406</td>\n    </tr>\n    <tr>\n      <th>forth</th>\n      <td>-0.082334</td>\n      <td>0.640348</td>\n    </tr>\n    <tr>\n      <th>fifth</th>\n      <td>0.129797</td>\n      <td>-1.121657</td>\n    </tr>\n    <tr>\n      <th>sixth</th>\n      <td>0.151908</td>\n      <td>-0.621642</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.T.groupby(lst).mean().T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.664805Z",
     "end_time": "2024-05-09T20:43:35.830126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_3948\\1940682838.py:2: FutureWarning: DataFrame.groupby with axis=1 is deprecated. Do `frame.T.groupby(...)` without axis instead.\n",
      "  data.groupby(mapping, axis=1).mean()\n"
     ]
    },
    {
     "data": {
      "text/plain": "             one       two\nfirst   1.214585 -0.571290\nsecond  0.303265 -0.795197\nthird  -0.525641 -0.238406\nforth  -0.082334  0.640348\nfifth   0.129797 -1.121657\nsixth   0.151908 -0.621642",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>first</th>\n      <td>1.214585</td>\n      <td>-0.571290</td>\n    </tr>\n    <tr>\n      <th>second</th>\n      <td>0.303265</td>\n      <td>-0.795197</td>\n    </tr>\n    <tr>\n      <th>third</th>\n      <td>-0.525641</td>\n      <td>-0.238406</td>\n    </tr>\n    <tr>\n      <th>forth</th>\n      <td>-0.082334</td>\n      <td>0.640348</td>\n    </tr>\n    <tr>\n      <th>fifth</th>\n      <td>0.129797</td>\n      <td>-1.121657</td>\n    </tr>\n    <tr>\n      <th>sixth</th>\n      <td>0.151908</td>\n      <td>-0.621642</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mapping = {'a': \"one\", 'b': \"one\", 'c': \"one\", 'd': \"two\", 'e': \"two\", 'f': \"two\"}\n",
    "data.groupby(mapping, axis=1).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.680874Z",
     "end_time": "2024-05-09T20:43:35.830126Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "             one       two\nfirst   1.214585 -0.571290\nsecond  0.303265 -0.795197\nthird  -0.525641 -0.238406\nforth  -0.082334  0.640348\nfifth   0.129797 -1.121657\nsixth   0.151908 -0.621642",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>first</th>\n      <td>1.214585</td>\n      <td>-0.571290</td>\n    </tr>\n    <tr>\n      <th>second</th>\n      <td>0.303265</td>\n      <td>-0.795197</td>\n    </tr>\n    <tr>\n      <th>third</th>\n      <td>-0.525641</td>\n      <td>-0.238406</td>\n    </tr>\n    <tr>\n      <th>forth</th>\n      <td>-0.082334</td>\n      <td>0.640348</td>\n    </tr>\n    <tr>\n      <th>fifth</th>\n      <td>0.129797</td>\n      <td>-1.121657</td>\n    </tr>\n    <tr>\n      <th>sixth</th>\n      <td>0.151908</td>\n      <td>-0.621642</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.T.groupby(mapping).mean().T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.693810Z",
     "end_time": "2024-05-09T20:43:35.866840Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_3948\\3836762088.py:2: FutureWarning: DataFrame.groupby with axis=1 is deprecated. Do `frame.T.groupby(...)` without axis instead.\n",
      "  data.groupby(series_map, axis=1).mean()\n"
     ]
    },
    {
     "data": {
      "text/plain": "             one       two\nfirst   1.214585 -0.571290\nsecond  0.303265 -0.795197\nthird  -0.525641 -0.238406\nforth  -0.082334  0.640348\nfifth   0.129797 -1.121657\nsixth   0.151908 -0.621642",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>first</th>\n      <td>1.214585</td>\n      <td>-0.571290</td>\n    </tr>\n    <tr>\n      <th>second</th>\n      <td>0.303265</td>\n      <td>-0.795197</td>\n    </tr>\n    <tr>\n      <th>third</th>\n      <td>-0.525641</td>\n      <td>-0.238406</td>\n    </tr>\n    <tr>\n      <th>forth</th>\n      <td>-0.082334</td>\n      <td>0.640348</td>\n    </tr>\n    <tr>\n      <th>fifth</th>\n      <td>0.129797</td>\n      <td>-1.121657</td>\n    </tr>\n    <tr>\n      <th>sixth</th>\n      <td>0.151908</td>\n      <td>-0.621642</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "series_map = pd.Series(mapping)\n",
    "data.groupby(series_map, axis=1).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.717049Z",
     "end_time": "2024-05-09T20:43:35.866840Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "             one       two\nfirst   1.214585 -0.571290\nsecond  0.303265 -0.795197\nthird  -0.525641 -0.238406\nforth  -0.082334  0.640348\nfifth   0.129797 -1.121657\nsixth   0.151908 -0.621642",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>one</th>\n      <th>two</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>first</th>\n      <td>1.214585</td>\n      <td>-0.571290</td>\n    </tr>\n    <tr>\n      <th>second</th>\n      <td>0.303265</td>\n      <td>-0.795197</td>\n    </tr>\n    <tr>\n      <th>third</th>\n      <td>-0.525641</td>\n      <td>-0.238406</td>\n    </tr>\n    <tr>\n      <th>forth</th>\n      <td>-0.082334</td>\n      <td>0.640348</td>\n    </tr>\n    <tr>\n      <th>fifth</th>\n      <td>0.129797</td>\n      <td>-1.121657</td>\n    </tr>\n    <tr>\n      <th>sixth</th>\n      <td>0.151908</td>\n      <td>-0.621642</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.T.groupby(series_map).mean().T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.734021Z",
     "end_time": "2024-05-09T20:43:35.866840Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "              a         b         c         d         e         f\nFalse  0.297751  0.544228  0.067817 -0.513818 -0.217465 -1.654308\nTrue  -0.054629  0.113294  0.474325 -0.480304 -0.284543 -0.382741",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>False</th>\n      <td>0.297751</td>\n      <td>0.544228</td>\n      <td>0.067817</td>\n      <td>-0.513818</td>\n      <td>-0.217465</td>\n      <td>-1.654308</td>\n    </tr>\n    <tr>\n      <th>True</th>\n      <td>-0.054629</td>\n      <td>0.113294</td>\n      <td>0.474325</td>\n      <td>-0.480304</td>\n      <td>-0.284543</td>\n      <td>-0.382741</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def is_t(name):  #创建一个函数，判断对象中是否含有字母“t”。\n",
    "    if \"t\" in name:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "\n",
    "data.groupby(is_t).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.752053Z",
     "end_time": "2024-05-09T20:43:35.951716Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "          a         b         c         d         e         f\n5 -0.054629  0.113294  0.474325 -0.480304 -0.284543 -0.382741\n6  0.297751  0.544228  0.067817 -0.513818 -0.217465 -1.654308",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>5</th>\n      <td>-0.054629</td>\n      <td>0.113294</td>\n      <td>0.474325</td>\n      <td>-0.480304</td>\n      <td>-0.284543</td>\n      <td>-0.382741</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0.297751</td>\n      <td>0.544228</td>\n      <td>0.067817</td>\n      <td>-0.513818</td>\n      <td>-0.217465</td>\n      <td>-1.654308</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(len).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.779078Z",
     "end_time": "2024-05-09T20:43:35.964461Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "                a         b         c         d         e         f\nFalse B  0.297751  0.544228  0.067817 -0.513818 -0.217465 -1.654308\nTrue  A  0.277570  0.689640  0.731167 -0.334556  0.560163 -0.122021\n      B  0.453056 -0.502912  0.439248 -0.849174 -1.277251 -1.238545\n      C -0.640670 -0.154949  0.235021 -0.441617 -0.632895 -0.215560",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>a</th>\n      <th>b</th>\n      <th>c</th>\n      <th>d</th>\n      <th>e</th>\n      <th>f</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>False</th>\n      <th>B</th>\n      <td>0.297751</td>\n      <td>0.544228</td>\n      <td>0.067817</td>\n      <td>-0.513818</td>\n      <td>-0.217465</td>\n      <td>-1.654308</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">True</th>\n      <th>A</th>\n      <td>0.277570</td>\n      <td>0.689640</td>\n      <td>0.731167</td>\n      <td>-0.334556</td>\n      <td>0.560163</td>\n      <td>-0.122021</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>0.453056</td>\n      <td>-0.502912</td>\n      <td>0.439248</td>\n      <td>-0.849174</td>\n      <td>-1.277251</td>\n      <td>-1.238545</td>\n    </tr>\n    <tr>\n      <th>C</th>\n      <td>-0.640670</td>\n      <td>-0.154949</td>\n      <td>0.235021</td>\n      <td>-0.441617</td>\n      <td>-0.632895</td>\n      <td>-0.215560</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index_lst = ['A', 'B', 'C', 'A', 'B', 'C']\n",
    "data.groupby([is_t, index_lst]).mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.795030Z",
     "end_time": "2024-05-09T20:43:36.082647Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "         count       mean       std   min   25%   50%   75%   max\nteacher                                                          \nNetwon     3.0  88.333333  7.637626  80.0  85.0  90.0  92.5  95.0\nPascal     3.0  76.666667  7.637626  70.0  72.5  75.0  80.0  85.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>count</th>\n      <th>mean</th>\n      <th>std</th>\n      <th>min</th>\n      <th>25%</th>\n      <th>50%</th>\n      <th>75%</th>\n      <th>max</th>\n    </tr>\n    <tr>\n      <th>teacher</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Netwon</th>\n      <td>3.0</td>\n      <td>88.333333</td>\n      <td>7.637626</td>\n      <td>80.0</td>\n      <td>85.0</td>\n      <td>90.0</td>\n      <td>92.5</td>\n      <td>95.0</td>\n    </tr>\n    <tr>\n      <th>Pascal</th>\n      <td>3.0</td>\n      <td>76.666667</td>\n      <td>7.637626</td>\n      <td>70.0</td>\n      <td>72.5</td>\n      <td>75.0</td>\n      <td>80.0</td>\n      <td>85.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('teacher')['score'].describe()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.823746Z",
     "end_time": "2024-05-09T20:43:36.136868Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "             score                    rank              \n              mean       std max      mean       std max\nteacher                                                 \nNetwon   88.333333  7.637626  95  6.000000  2.645751   9\nPascal   76.666667  7.637626  85  4.666667  2.516611   7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">score</th>\n      <th colspan=\"3\" halign=\"left\">rank</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>mean</th>\n      <th>std</th>\n      <th>max</th>\n      <th>mean</th>\n      <th>std</th>\n      <th>max</th>\n    </tr>\n    <tr>\n      <th>teacher</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Netwon</th>\n      <td>88.333333</td>\n      <td>7.637626</td>\n      <td>95</td>\n      <td>6.000000</td>\n      <td>2.645751</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>Pascal</th>\n      <td>76.666667</td>\n      <td>7.637626</td>\n      <td>85</td>\n      <td>4.666667</td>\n      <td>2.516611</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['teacher', 'score', 'rank']].groupby(\"teacher\").aggregate(['mean', 'std', 'max'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.852534Z",
     "end_time": "2024-05-09T20:43:36.230563Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_3948\\4152580082.py:1: FutureWarning: The provided callable <function median at 0x000002C0C86A13F0> is currently using DataFrameGroupBy.median. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"median\" instead.\n",
      "  df[['teacher', 'score', 'rank']].groupby(\"teacher\").aggregate(np.median)\n"
     ]
    },
    {
     "data": {
      "text/plain": "         score  rank\nteacher             \nNetwon    90.0   5.0\nPascal    75.0   5.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n      <th>rank</th>\n    </tr>\n    <tr>\n      <th>teacher</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Netwon</th>\n      <td>90.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>Pascal</th>\n      <td>75.0</td>\n      <td>5.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['teacher', 'score', 'rank']].groupby(\"teacher\").aggregate(np.median)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.883058Z",
     "end_time": "2024-05-09T20:43:36.235023Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "         score  rank\nteacher             \nNetwon    90.0   5.0\nPascal    75.0   5.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n      <th>rank</th>\n    </tr>\n    <tr>\n      <th>teacher</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Netwon</th>\n      <td>90.0</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>Pascal</th>\n      <td>75.0</td>\n      <td>5.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['teacher', 'score', 'rank']].groupby(\"teacher\").median()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.913473Z",
     "end_time": "2024-05-09T20:43:36.255629Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "teacher\nNetwon    15\nPascal    15\nName: score, dtype: int64"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def distance(x):\n",
    "    return x.max() - x.min()\n",
    "\n",
    "\n",
    "df.groupby('teacher')['score'].aggregate(distance)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.928169Z",
     "end_time": "2024-05-09T20:43:36.255629Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_3948\\1441987240.py:1: FutureWarning: The provided callable <function mean at 0x000002C0C8566680> is currently using SeriesGroupBy.mean. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"mean\" instead.\n",
      "  df.groupby('teacher').aggregate({\"score\": np.mean, 'rank': np.median})\n",
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_3948\\1441987240.py:1: FutureWarning: The provided callable <function median at 0x000002C0C86A13F0> is currently using SeriesGroupBy.median. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"median\" instead.\n",
      "  df.groupby('teacher').aggregate({\"score\": np.mean, 'rank': np.median})\n"
     ]
    },
    {
     "data": {
      "text/plain": "             score  rank\nteacher                 \nNetwon   88.333333   5.0\nPascal   76.666667   5.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n      <th>rank</th>\n    </tr>\n    <tr>\n      <th>teacher</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Netwon</th>\n      <td>88.333333</td>\n      <td>5.0</td>\n    </tr>\n    <tr>\n      <th>Pascal</th>\n      <td>76.666667</td>\n      <td>5.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('teacher').aggregate({\"score\": np.mean, 'rank': np.median})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.955853Z",
     "end_time": "2024-05-09T20:43:36.276966Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_3948\\1890627739.py:1: FutureWarning: The provided callable <function mean at 0x000002C0C8566680> is currently using SeriesGroupBy.mean. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"mean\" instead.\n",
      "  df.groupby('teacher').aggregate({\"score\": ['max', 'min', np.mean], 'rank': 'std'})\n"
     ]
    },
    {
     "data": {
      "text/plain": "        score                     rank\n          max min       mean       std\nteacher                               \nNetwon     95  80  88.333333  2.645751\nPascal     85  70  76.666667  2.516611",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">score</th>\n      <th>rank</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>max</th>\n      <th>min</th>\n      <th>mean</th>\n      <th>std</th>\n    </tr>\n    <tr>\n      <th>teacher</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Netwon</th>\n      <td>95</td>\n      <td>80</td>\n      <td>88.333333</td>\n      <td>2.645751</td>\n    </tr>\n    <tr>\n      <th>Pascal</th>\n      <td>85</td>\n      <td>70</td>\n      <td>76.666667</td>\n      <td>2.516611</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('teacher').aggregate({\"score\": ['max', 'min', np.mean], 'rank': 'std'})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:35.990005Z",
     "end_time": "2024-05-09T20:43:36.293897Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "data": {
      "text/plain": "   subject  score teacher  rank\n0     math     90  Netwon     4\n1  physics     80  Netwon     5\n2  english     70  Pascal     2\n3     math     95  Netwon     9\n4  physics     85  Pascal     7\n5  english     75  Pascal     5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>subject</th>\n      <th>score</th>\n      <th>teacher</th>\n      <th>rank</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>math</td>\n      <td>90</td>\n      <td>Netwon</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>physics</td>\n      <td>80</td>\n      <td>Netwon</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>english</td>\n      <td>70</td>\n      <td>Pascal</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>math</td>\n      <td>95</td>\n      <td>Netwon</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>physics</td>\n      <td>85</td>\n      <td>Pascal</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>english</td>\n      <td>75</td>\n      <td>Pascal</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.007994Z",
     "end_time": "2024-05-09T20:43:36.293897Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "data": {
      "text/plain": "         score  rank\nsubject             \nenglish   72.5   3.5\nmath      92.5   6.5\nphysics   82.5   6.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n      <th>rank</th>\n    </tr>\n    <tr>\n      <th>subject</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>english</th>\n      <td>72.5</td>\n      <td>3.5</td>\n    </tr>\n    <tr>\n      <th>math</th>\n      <td>92.5</td>\n      <td>6.5</td>\n    </tr>\n    <tr>\n      <th>physics</th>\n      <td>82.5</td>\n      <td>6.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['subject', 'score', 'rank']].groupby('subject').mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.038689Z",
     "end_time": "2024-05-09T20:43:36.310257Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "   subject  score teacher  rank\n0     math     90  Netwon     4\n1  physics     80  Netwon     5\n3     math     95  Netwon     9\n4  physics     85  Pascal     7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>subject</th>\n      <th>score</th>\n      <th>teacher</th>\n      <th>rank</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>math</td>\n      <td>90</td>\n      <td>Netwon</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>physics</td>\n      <td>80</td>\n      <td>Netwon</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>math</td>\n      <td>95</td>\n      <td>Netwon</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>physics</td>\n      <td>85</td>\n      <td>Pascal</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def high_rank(x):\n",
    "    return x['rank'].mean() > 5\n",
    "\n",
    "\n",
    "df.groupby('subject').filter(high_rank)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.061744Z",
     "end_time": "2024-05-09T20:43:36.364926Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "data": {
      "text/plain": "   subject  score teacher  rank\n0     math     90  Netwon     4\n1  physics     80  Netwon     5\n3     math     95  Netwon     9\n4  physics     85  Pascal     7",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>subject</th>\n      <th>score</th>\n      <th>teacher</th>\n      <th>rank</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>math</td>\n      <td>90</td>\n      <td>Netwon</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>physics</td>\n      <td>80</td>\n      <td>Netwon</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>math</td>\n      <td>95</td>\n      <td>Netwon</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>physics</td>\n      <td>85</td>\n      <td>Pascal</td>\n      <td>7</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('subject').filter(lambda x: x['rank'].mean() > 5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.087365Z",
     "end_time": "2024-05-09T20:43:36.410288Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "data": {
      "text/plain": "   score  rank\n0   -2.5  -2.5\n1   -2.5  -1.0\n2   -2.5  -1.5\n3    2.5   2.5\n4    2.5   1.0\n5    2.5   1.5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n      <th>rank</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>-2.5</td>\n      <td>-2.5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>-2.5</td>\n      <td>-1.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>-2.5</td>\n      <td>-1.5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2.5</td>\n      <td>2.5</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2.5</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>2.5</td>\n      <td>1.5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[['subject', 'score', 'rank']].groupby(\"subject\").transform(lambda x: x - x.mean())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.112824Z",
     "end_time": "2024-05-09T20:43:36.410288Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "data": {
      "text/plain": "           subject  score teacher  rank  rank_score\nsubject                                            \nenglish 2  english     70  Pascal     2  148.492424\n        5  english     75  Pascal     5  159.099026\nmath    0     math     90  Netwon     4  318.198052\n        3     math     95  Netwon     9  335.875721\nphysics 1  physics     80  Netwon     5  113.137085\n        4  physics     85  Pascal     7  120.208153",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>subject</th>\n      <th>score</th>\n      <th>teacher</th>\n      <th>rank</th>\n      <th>rank_score</th>\n    </tr>\n    <tr>\n      <th>subject</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">english</th>\n      <th>2</th>\n      <td>english</td>\n      <td>70</td>\n      <td>Pascal</td>\n      <td>2</td>\n      <td>148.492424</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>english</td>\n      <td>75</td>\n      <td>Pascal</td>\n      <td>5</td>\n      <td>159.099026</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">math</th>\n      <th>0</th>\n      <td>math</td>\n      <td>90</td>\n      <td>Netwon</td>\n      <td>4</td>\n      <td>318.198052</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>math</td>\n      <td>95</td>\n      <td>Netwon</td>\n      <td>9</td>\n      <td>335.875721</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">physics</th>\n      <th>1</th>\n      <td>physics</td>\n      <td>80</td>\n      <td>Netwon</td>\n      <td>5</td>\n      <td>113.137085</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>physics</td>\n      <td>85</td>\n      <td>Pascal</td>\n      <td>7</td>\n      <td>120.208153</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_subject = df.groupby(\"subject\")\n",
    "\n",
    "\n",
    "def rank_score(x):  #创建一个函数\n",
    "    x['rank_score'] = x['score'] * x['rank'].std()\n",
    "    return x\n",
    "\n",
    "\n",
    "df_subject.apply(rank_score)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.130479Z",
     "end_time": "2024-05-09T20:43:36.458671Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "data": {
      "text/plain": "subject\nenglish    2.121320\nmath       3.535534\nphysics    1.414214\nName: rank, dtype: float64"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_subject['rank'].std()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.159236Z",
     "end_time": "2024-05-09T20:43:36.574420Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.应用举例"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "   survived  pclass     sex   age  sibsp  parch     fare embarked  class  \\\n0         0       3    male  22.0      1      0   7.2500        S  Third   \n1         1       1  female  38.0      1      0  71.2833        C  First   \n2         1       3  female  26.0      0      0   7.9250        S  Third   \n3         1       1  female  35.0      1      0  53.1000        S  First   \n4         0       3    male  35.0      0      0   8.0500        S  Third   \n\n     who  adult_male deck  embark_town alive  alone  \n0    man        True  NaN  Southampton    no  False  \n1  woman       False    C    Cherbourg   yes  False  \n2  woman       False  NaN  Southampton   yes   True  \n3  woman       False    C  Southampton   yes  False  \n4    man        True  NaN  Southampton    no   True  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>survived</th>\n      <th>pclass</th>\n      <th>sex</th>\n      <th>age</th>\n      <th>sibsp</th>\n      <th>parch</th>\n      <th>fare</th>\n      <th>embarked</th>\n      <th>class</th>\n      <th>who</th>\n      <th>adult_male</th>\n      <th>deck</th>\n      <th>embark_town</th>\n      <th>alive</th>\n      <th>alone</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>3</td>\n      <td>male</td>\n      <td>22.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>7.2500</td>\n      <td>S</td>\n      <td>Third</td>\n      <td>man</td>\n      <td>True</td>\n      <td>NaN</td>\n      <td>Southampton</td>\n      <td>no</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>1</td>\n      <td>female</td>\n      <td>38.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>71.2833</td>\n      <td>C</td>\n      <td>First</td>\n      <td>woman</td>\n      <td>False</td>\n      <td>C</td>\n      <td>Cherbourg</td>\n      <td>yes</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>3</td>\n      <td>female</td>\n      <td>26.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>7.9250</td>\n      <td>S</td>\n      <td>Third</td>\n      <td>woman</td>\n      <td>False</td>\n      <td>NaN</td>\n      <td>Southampton</td>\n      <td>yes</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>1</td>\n      <td>female</td>\n      <td>35.0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>53.1000</td>\n      <td>S</td>\n      <td>First</td>\n      <td>woman</td>\n      <td>False</td>\n      <td>C</td>\n      <td>Southampton</td>\n      <td>yes</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>3</td>\n      <td>male</td>\n      <td>35.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>8.0500</td>\n      <td>S</td>\n      <td>Third</td>\n      <td>man</td>\n      <td>True</td>\n      <td>NaN</td>\n      <td>Southampton</td>\n      <td>no</td>\n      <td>True</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "titanic = sns.load_dataset('titanic')\n",
    "titanic.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.175548Z",
     "end_time": "2024-05-09T20:43:36.735468Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "sex\nfemale    0.742038\nmale      0.188908\nName: survived, dtype: float64"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.groupby('sex')['survived'].mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.210582Z",
     "end_time": "2024-05-09T20:43:36.735468Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\58248\\AppData\\Local\\Temp\\ipykernel_3948\\653475927.py:1: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  titanic.groupby(['sex', 'class'])['survived'].aggregate('mean').unstack()\n"
     ]
    },
    {
     "data": {
      "text/plain": "class      First    Second     Third\nsex                                 \nfemale  0.968085  0.921053  0.500000\nmale    0.368852  0.157407  0.135447",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>class</th>\n      <th>First</th>\n      <th>Second</th>\n      <th>Third</th>\n    </tr>\n    <tr>\n      <th>sex</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>female</th>\n      <td>0.968085</td>\n      <td>0.921053</td>\n      <td>0.500000</td>\n    </tr>\n    <tr>\n      <th>male</th>\n      <td>0.368852</td>\n      <td>0.157407</td>\n      <td>0.135447</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.groupby(['sex', 'class'])['survived'].aggregate('mean').unstack()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.231748Z",
     "end_time": "2024-05-09T20:43:36.735468Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "class      First    Second     Third\nsex                                 \nfemale  0.968085  0.921053  0.500000\nmale    0.368852  0.157407  0.135447",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>class</th>\n      <th>First</th>\n      <th>Second</th>\n      <th>Third</th>\n    </tr>\n    <tr>\n      <th>sex</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>female</th>\n      <td>0.968085</td>\n      <td>0.921053</td>\n      <td>0.500000</td>\n    </tr>\n    <tr>\n      <th>male</th>\n      <td>0.368852</td>\n      <td>0.157407</td>\n      <td>0.135447</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.pivot_table('survived', index='sex', columns='class')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.251731Z",
     "end_time": "2024-05-09T20:43:36.841219Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "class               First    Second     Third\nsex    age                                   \nfemale (0, 18]   0.909091  1.000000  0.511628\n       (18, 60]  0.972222  0.900000  0.413793\n       (60, 80]  1.000000       NaN  1.000000\nmale   (0, 18]   0.800000  0.600000  0.215686\n       (18, 60]  0.416667  0.061728  0.136364\n       (60, 80]  0.083333  0.333333  0.000000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>class</th>\n      <th>First</th>\n      <th>Second</th>\n      <th>Third</th>\n    </tr>\n    <tr>\n      <th>sex</th>\n      <th>age</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">female</th>\n      <th>(0, 18]</th>\n      <td>0.909091</td>\n      <td>1.000000</td>\n      <td>0.511628</td>\n    </tr>\n    <tr>\n      <th>(18, 60]</th>\n      <td>0.972222</td>\n      <td>0.900000</td>\n      <td>0.413793</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>1.000000</td>\n      <td>NaN</td>\n      <td>1.000000</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">male</th>\n      <th>(0, 18]</th>\n      <td>0.800000</td>\n      <td>0.600000</td>\n      <td>0.215686</td>\n    </tr>\n    <tr>\n      <th>(18, 60]</th>\n      <td>0.416667</td>\n      <td>0.061728</td>\n      <td>0.136364</td>\n    </tr>\n    <tr>\n      <th>(60, 80]</th>\n      <td>0.083333</td>\n      <td>0.333333</td>\n      <td>0.000000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ages = pd.cut(titanic['age'], [0, 18, 60, 80])\n",
    "titanic.pivot_table('survived', index=['sex', ages], columns='class')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.272187Z",
     "end_time": "2024-05-09T20:43:36.841921Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "data": {
      "text/plain": "0    (10, 30]\n1    (10, 30]\n2    (10, 30]\n3    (30, 40]\n4    (30, 40]\ndtype: category\nCategories (2, interval[int64, right]): [(10, 30] < (30, 40]]"
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = pd.Series([20, 25, 30, 35, 40])\n",
    "bins = [10, 30, 40]\n",
    "pd.cut(x, bins)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.308257Z",
     "end_time": "2024-05-09T20:43:36.841921Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-09T20:43:36.329919Z",
     "end_time": "2024-05-09T20:43:36.841921Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
